package com.example.Set;

import java.util.TreeSet;

public class Test3Set {
    public static void main(String[] args) {
        TreeSet<Students> ts = new TreeSet<>();
        Students s1 = new Students("xiaohei", 88, 89, 90);
        Students s2 = new Students("dahei", 85, 90, 91);
        Students s3 = new Students("erhei", 90, 89, 100);
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        for (Students t : ts) {
            System.out.println(t);
        }
//        Student{name='erhei', Chinese=90, Math=89, English=100}总分为279
//        Student{name='xiaohei', Chinese=88, Math=89, English=90}总分为267
//        Student{name='dahei', Chinese=85, Math=90, English=91}总分为266

    }
}

class Students implements Comparable<Students> {
    private String name;
    private int Chinese;
    private int Math;
    private int English;

    public Students() {
    }

    public Students(String name, int chinese, int math, int english) {
        this.name = name;
        Chinese = chinese;
        Math = math;
        English = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return Chinese;
    }

    public void setChinese(int chinese) {
        Chinese = chinese;
    }

    public int getMath() {
        return Math;
    }

    public void setMath(int math) {
        Math = math;
    }

    public int getEnglish() {
        return English;
    }

    public void setEnglish(int english) {
        English = english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", Chinese=" + Chinese +
                ", Math=" + Math +
                ", English=" + English +
                '}' + "总分为" + getSum();
    }

    public int getSum() {
        return Chinese + Math + English;
    }

    @Override
    public int compareTo(Students o) {
        //按照总分进行排序
        //int result =this.getchinese()+ this.getMath()+ this.getEnglish()
        int result = o.getSum() - this.getSum();
        //次要条件
        //总分一样，就比较语文成绩
        result = result == 0 ? o.getChinese() - this.getChinese() : result;
        //语文成绩一样，就比较数学成绩
        result = result == 0 ? o.getMath() - this.getMath() : result;
        //如果数学成绩一样，就比较英文成绩
        result = result == 0 ? o.getEnglish() - this.getEnglish() : result;
        //如果成绩都一样，则按照姓名进行排序
        return result;
    }
}